package com.ai.planttrees.service.impl;

import com.ai.planttrees.dao.*;
import com.ai.planttrees.model.*;
import com.ai.planttrees.service.UserTreeService;
import com.ai.planttrees.core.AbstractService;
import com.ai.planttrees.util.BaseUtil;
import com.ai.planttrees.util.DES;
import com.ai.planttrees.util.Result;
import org.apache.catalina.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;


/**
 * Created by CodeGenerator on 2018/04/23.
 */
@Service
public class UserTreeServiceImpl extends AbstractService<UserTree> implements UserTreeService {
    private Logger logger = LoggerFactory.getLogger(UserTreeServiceImpl.class);
    @Resource
    private PrizeMapper prizeMapper;
    @Resource
    private PlantMapper plantMapper;
    @Resource
    private UserTreeMapper userTreeMapper;


    @Override
    @Transactional
    public Result selUserInfo(String phone) {
        logger.info("按外部用户Id查询用户活动信息，参数：{}",phone);
        UserTree qs = new UserTree();
        qs.setPhone(phone);
        UserTree userTree = userTreeMapper.selectOne(qs);
        logger.info("结果：{}",userTree);
        if(userTree==null){
            userTree = new UserTree();
            userTree.setId(BaseUtil.getUUID());
            userTree.setPhone(phone);
            String decryptDES = null;
            try {
                decryptDES = DES.decryptDES(phone, DES.key);
            } catch (Exception e) {
                e.printStackTrace();
            }
            userTree.setPhoneReal(decryptDES.split("=")[1]);
            userTree.setFlow(0L);
            userTree.setTreeNum(0);
            userTree.setCreateTime(new Date());
            try{
                int i = userTreeMapper.insertSelective(userTree);
                logger.info("未找到指定用户,添加新用户活动信息，结果{}",i);
            }catch (Exception e){
                e.printStackTrace();
            }
        }

        Map map = new HashMap<>();
        map.put("userId",userTree.getPhone());
        logger.info("查询用户今日，阳光，除虫，浇水次数");
        map.put("plantType","sunshine");
        Integer scount = plantMapper.countTodayByUserId(map);
        logger.info("阳光今日次数：{}",scount);
        userTree.setSunshineCount(scount);
        map.put("plantType","disinsection");
        Integer dcount = plantMapper.countTodayByUserId(map);
        logger.info("除虫今日次数：{}",dcount);
        userTree.setDisinsectionCount(dcount);
        map.put("plantType","watering");
        Integer wcount = plantMapper.countTodayByUserId(map);
        logger.info("浇水今日次数：{}",wcount);
        userTree.setWateringCount(wcount);
        return Result.success(userTree);
    }

    @Override
    @Transactional
    public Result selUserInfoPrize(UserTree userTree) {
        logger.info("查询用户活动已兑换奖品");
        List<Prize> userPrizeList = prizeMapper.selByUserId(userTree.getPhone());
        logger.info("结果：{}",userPrizeList);
        userTree.setUserPrizeList(userPrizeList);
        logger.info("查询奖品列表,标注可否兑换");
        List<Prize> prizeList = prizeMapper.selByUserIdToPrize(userTree.getPhone());
        for (Prize prize : prizeList) {
            for (Prize prize1 : userPrizeList) {
                if(prize.getId().equals(prize1.getId())){
                    prize.setIsRecord((byte) 2); //已兑换
                    if(prize.getPrizeTotal()<=0){//已兑换且没有剩余奖品
                        prize.setIsRecord((byte) 3);
                    }
                }
            }
        }
        logger.info("结果：{}",prizeList);
        userTree.setPrizeList(prizeList);
        return Result.success(userTree);
    }
}
